package com.md24housecall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.androidquery.AQuery;
import com.md24housecall.adapter.FragmentAdapter;
import com.md24housecall.manager.CacheManager;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;
import com.md24housecall.util.Utils;
import com.md24housecall.util.Utils.OnRequestCategoryListner;
import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends com.md24housecall.fibromyalgiadiagnostic.MainActivity {
	
	private FragmentAdapter mAdapter;
	private ViewPager mPager;
	private TabPageIndicator mIndicator;
	private GlobalConstant constant=GlobalConstant.getInstance();
	private Utils util = Utils.getInstance();
	private AQuery aQuery;
	private CacheManager cacheManager;
	private int totalCategory=0;
	private int totalPartner=0;
	private String code = "";
	private Handler mHandler=new Handler();
	private Runnable mRunnable;
	private String[]contents;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		constant.isUpdateIhealthUser=true;
		constant.isUpdatePatient=true;
		code = util.getCode(this);
		final FragmentActivity act=this;
		mRunnable=new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(!code.equals("")){
					//		CONTENT[0]=act.getResources().getString(R.string.act_request_call);
						contents = getResources().getStringArray(R.array.tabs_name_login);
						if(constant.userType==constant.CENTER_USER)
							contents[contents.length-1]=getResources().getString(R.string.center_tab_name);
						constant.code=code;
					}else{
						contents = getResources().getStringArray(R.array.tabs_name);
					}
					mAdapter = new FragmentAdapter(act,contents);
					mPager = (ViewPager) findViewById(R.id.pager);
					mPager.setAdapter(mAdapter);
//					if(!util.getCode(this).equals("")){
////						CONTENT[0]=act.getResources().getString(R.string.act_request_call);
//						count = getResources().getStringArray(R.array.tabs_name_login).length;
//						
//					}else{
//						count =getResources().getStringArray(R.array.tabs_name).length;
//					}
//					if(mAdapter.getCount()<count)
//						mAdapter.notifyDataSetChanged();

					mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
					mIndicator.setViewPager(mPager);

					mPager.setOffscreenPageLimit(mAdapter.getCount());
//					constant.mainActivity=this;
//					MyLog.d(GlobalConstant.TAG, "main activity");
					aQuery=new AQuery(act);
					cacheManager=CacheManager.getInstance(act);
					totalCategory=cacheManager.getTotalPartnerCategory();
					totalPartner=cacheManager.getTotalPartners();
					if(totalPartner==0 || constant.isRetryRequestPartner)
						util.getPartnerFromApi(aQuery, cacheManager, false);
					if(totalCategory==0 || constant.isRetryRequestCategory){
						util.getPartnerCategoryFromApi(aQuery, cacheManager, false);
						util.setOnRequestCommplete(new OnRequestCategoryListner() {
							
							@Override
							public void onRequestComplete() {
								// TODO Auto-generated method stub
								MyLog.d(GlobalConstant.TAG, "on retry category complete");
								sendBroadcast(new Intent(constant.ACTON_REFRESH));
							}
						});
					}
			}
		};
		mHandler.postDelayed(mRunnable, 500);
		
	}

	private void logout(){
		util.clearAllData(this);
		finish();
		startActivity(new Intent(this,MainActivity.class));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		if(!util.getCode(this).equals("")){
			MenuInflater inflater = getMenuInflater();
		    inflater.inflate(R.menu.main, menu);
		    return true;
		}else
			return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
			case R.id.action_logout:
				logout();
				break;
			default:
	            return super.onOptionsItemSelected(item);
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		mHandler.removeCallbacks(mRunnable);
		super.onDestroy();
		MyLog.d(GlobalConstant.TAG, "onDestroy MainActivity");
//		cacheManager.close();
	}
}
